package com.smartshopping.iu;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.smartshopping.model.BillItem;
import com.smartshopping.model.DataBaseHelper;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BillViewActivity extends Activity {

	private DataBaseHelper myDbHelper;
	private BillItem bill;
	private Intent resultData;
	private int totalPrice = 0;
			
	private String sql = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill_view);
		
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		bill = new BillItem(extras.getParcelableArrayList("list"),Integer.parseInt(extras.getString("coopID")),Integer.parseInt(extras.getString("marketID")));
		
		connectionDatabase();
		createTheHeader();
		createTheListItem();
	}

	public void connectionDatabase()
	{
		myDbHelper = new DataBaseHelper(this);
		try 
		{
			myDbHelper.createDataBase(this);
			myDbHelper.openDataBase();
		} catch (IOException ioe) 
		{
		 		throw new Error("Unable to create database");
		}
	}
	
	//Create the title of the bill
	public void createTheHeader()
	{
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		SQLiteDatabase mdb = myDbHelper.getReadableDatabase();
		final String sql = "select * from market where market._id = " + Integer.toString(bill.getMarketId());
		Cursor mCursor = mdb.rawQuery(sql, null); 	
		if(mCursor.moveToFirst())
			((TextView)findViewById(R.id.bill_view_textView_market)).setText(mCursor.getString(1));
		mCursor.close();
		final String sql1 = "select * from coop where coop._id = " + Integer.toString(bill.getCoopId());
		mCursor = mdb.rawQuery(sql1, null); 	
		if(mCursor.moveToFirst())
			((TextView)findViewById(R.id.bill_view_textView_coop)).setText(mCursor.getString(1));
		((TextView)findViewById(R.id.bill_view_textView_date)).setText(extras.getString("date"));
		mCursor.close();
		mdb.close();
	}
	
	public void createTheListItem()
	{
		LinearLayout layout = (LinearLayout) findViewById(R.id.bill_view_LinearLayout);
		layout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"
		for (int index = 0; index < bill.getNumberItem(); index++) 
		{
			final int in = index;
			System.out.println(in);
		    LinearLayout row = new LinearLayout(this);
		    row.setOrientation(LinearLayout.HORIZONTAL);
		    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		    LinearLayout row1 = new LinearLayout(this);
		    row1.setOrientation(LinearLayout.HORIZONTAL);
		    row1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		    
		    //Name of good
		    TextView textviewName = new TextView(this);
		    textviewName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		    textviewName.setText(bill.getList().get(index).getName() + " ");
		    textviewName.setId(1 + (index * 3));
		    row.addView(textviewName);
		    
		  //Amount of good
		    TextView textviewAmount = new TextView(this);
		    textviewAmount.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		    textviewAmount.setText("SL: " + Integer.toString(bill.getList().get(index).getAmount()) + "\t");
		    textviewAmount.setId(2 + (index * 3));
		    row1.addView(textviewAmount);
		    
		    //Total price
		    TextView textviewSum = new TextView(this);
		    textviewSum.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		    textviewSum.setText("Giá: " + Integer.toString(bill.getList().get(index).getAmount() * bill.getList().get(index).getPrice()));
		    textviewSum.setId(3 + (index * 3));
		    row1.addView(textviewSum);
		    
		    layout.addView(row);
		    layout.addView(row1);
		    totalPrice += bill.getList().get(index).getAmount() * bill.getList().get(index).getPrice();
		}
		((TextView)findViewById(R.id.bill_view_textView_price)).setText("Tổng giá trị : " + Integer.toString(totalPrice));
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.bill, menu);
		return true;
	}

}
